/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.ui.preferences;

import org.eclipse.dltk.ui.preferences.AbstractConfigurationBlock;
import org.eclipse.dltk.ui.preferences.OverlayPreferenceStore;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class USGlobalPreferencesBlock extends AbstractConfigurationBlock {


    public USGlobalPreferencesBlock(OverlayPreferenceStore store) {
        super(store);
    }

    @SuppressWarnings("unused")
    private Control external;

    private Text udkHomeText;


    public Control createControl(Composite parent) {
        Composite composite = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        layout.numColumns = 1;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData());
        external = createExternalArea(composite);

        Dialog.applyDialogFont(parent);
        return composite;
    }

    protected Control createExternalArea(Composite parent) {

        Composite composite = new Composite(parent, SWT.NULL);
        GridLayout layout = new GridLayout();
        layout.marginWidth = 5;
        layout.marginHeight = 5;
        layout.numColumns = 3;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        createUDKHome(composite);

        initializeDefaults();
        return composite;
    }

    private void initializeDefaults() {
        udkHomeText.setText("C:\\UDK");
        
    }

    private void createUDKHome(Composite composite) {
        Label intro = new Label(composite, SWT.LEFT | SWT.WRAP);

        intro.setText("UDK Home:");
        GridData data = new GridData();
        data.horizontalSpan = 3;
        data.horizontalAlignment = GridData.FILL;
        data.widthHint = 300;
        intro.setLayoutData(data);

        new Label(composite, SWT.LEFT).setText("");
        udkHomeText = new Text(composite, SWT.BORDER);
        udkHomeText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        final Button b = new Button(composite, SWT.NONE);
        b.setText("Browse");
        data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        int widthHint = 50;
        data.widthHint = Math.max(widthHint, b.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
        b.setLayoutData(data);
        b.addListener(SWT.MouseDown, new Listener() {
            public void handleEvent(Event event) {
                DirectoryDialog d = new DirectoryDialog(b.getShell());
                d.setText(udkHomeText.getText());
                String file = d.open();
                if (file != null) {
                    setUdkHome(file);
                }
            }

        });
    }

    private void setUdkHome(String folder) {
        udkHomeText.setText(folder);
    }

}
